package com.shoppingmall.controller.portal;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by
 *  前台商品详情页面的展示 包含搜索等功能
 *  适用于用户查询  只能查询到已上架的商品
 * @author touni
 * @date 2019/3/19
 * @time 17:58
 */
@RestController
@RequestMapping("/product")
public class ProductController {


    /**
     * 存储用户的session的名称
     */
    @Value("${USER_SESSION}")
    private String userSessoin;


    @Autowired
    private ProductService productService;

    /**
     * 根据关键字和分类ID来查询商品列表
     * @param categoryId
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param orderBy
     * @return
     */
    @RequestMapping("/list")
    public ServletResponse listProductByCategoryIdAndKeyword(
                           HttpSession session,
                           Integer categoryId,String keyword,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                           @RequestParam(value = "orderBy",defaultValue = "price_desc")String orderBy){
        //todo 验证用户是否登录


        return productService.userListProductByCategoryIdAndKeyword(categoryId,keyword,pageNum,pageSize,orderBy);

    }


    /**
     * 根据商品的ID来查询商品的详细信息   tip 只能查看到上架的商品，即status == 1  直接在SQL中封装status = 1 的逻辑
     * @param productId
     * @return
     */
    @RequestMapping("/detail")
    public ServletResponse getProductDetail(Integer productId){
        // TODO 用户权限验证
        if (null == productId){
            return ServletResponse.createByErrorMessage("参数错误！！！");
        }


        return productService.userGetProductDetail(productId);
    }













}
